
#include <omp.h>

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <sys/time.h>
#include <time.h>

double omp_get_time(void)
{
    struct timeval tv;
    double t;

    gettimeofday(&tv, (struct timezone *)0);
    t = tv.tv_sec + (double)tv.tv_usec * 1e-6;

    return t;
}

int main(int argc, char **argv)
{
    int len = 100000000;
    double *x, *y, *z;
    double va_tm;

    if (argc == 2) {
        int tlen = atoi(argv[1]);

        if (tlen > 0) len = tlen;
    }

    /* malloc memory, no check */
    x = malloc(sizeof(*x) * len);
    y = malloc(sizeof(*y) * len);
    z = malloc(sizeof(*z) * len);

    va_tm = omp_get_time();

    #pragma omp parallel 
    {
        int i;
        int num_threads;
        int id;
        int blk;
        int start, end;

        num_threads = omp_get_num_threads();
        id = omp_get_thread_num();

        /* task division */
        blk = (len + num_threads - 1) / num_threads;

        /* starting and ending range */
        start = id * blk;
        end = start + blk;
        if (end > len) end = len;

        for (i = start; i < end; i++) {
            x[i] = i + 0.3;
            y[i] = i + M_PI;
        }

        for (i = start; i < end; i++) {
            z[i] = x[i] + y[i];
        }
    }

    va_tm = omp_get_time() - va_tm;
    printf("time: %g s\n", va_tm);

    free(x);
    free(y);
    free(z);

    return 0;
}
